#### ARM Trusted Firmware ####
EEL_MODS	+= atf
DEPS_TEE	+= atf

# $(info $(call deftargets_git,atf,atf))
$(eval $(call deftargets_git,atf,TF_A))
$(eval $(call deftargets_download,atf,TF_A,$$(TF_A_PKG),v$$(TF_A_VERSION)))

PHONY-atf += atf atf-clean

# DEBUG=1 -> $(O_TF_A_DIR)/qemu/debug/
# BUILD_BASE: output dir
atf: $(BUILD_DEPS) $(TF_A_BL3x_BINS)
	@$(MAKE_THIS) atf-prepare

	$(MAKE) $(JOB_OPT) \
		-C $(S_TF_A_DIR) \
		ARCH=$(TF_A_ARCH) \
		PLAT=$(TF_A_PLAT) \
		DEBUG=$(TF_A_DEBUG) \
		BUILD_BASE=$(O_TF_A_DIR) \
		CROSS_COMPILE=$(TC_PREFIX) \
		BL32=$(TF_A_BL32_BIN) \
		BL32_EXTRA1=$(TF_A_BL32_EXTRA1_BIN) \
		BL32_EXTRA2=$(TF_A_BL32_EXTRA2_BIN) \
		BL33=$(TF_A_BL33_BIN) \
		$(TF_A_CFG_OPTS) \
		$(TF_A_TARGETS)

	$(EELQ4)mkdir -p $(O_TF_A_DIR)/qemu/$(TF_A_OUTDIR)
	$(EELQ3)ln -sf $(TF_A_BL32_BIN)			$(O_TF_A_DIR)/qemu/$(TF_A_OUTDIR)/bl32.bin
	$(EELQ3)ln -sf $(TF_A_BL32_EXTRA1_BIN)	$(O_TF_A_DIR)/qemu/$(TF_A_OUTDIR)/bl32_extra1.bin
	$(EELQ3)ln -sf $(TF_A_BL32_EXTRA2_BIN)	$(O_TF_A_DIR)/qemu/$(TF_A_OUTDIR)/bl32_extra2.bin
	$(EELQ3)ln -sf $(TF_A_BL33_BIN)			$(O_TF_A_DIR)/qemu/$(TF_A_OUTDIR)/bl33.bin


# update atf if TF_A_BL3x_BINS have updated
$(TF_A_IMGS): $(TF_A_BL3x_BINS)
	$(EELQ3)$(MAKE_THIS) atf-clean
	$(EELQ2)$(MAKE_THIS) atf ARM_TEE=1

# FIXME: atf-clean
atf-clean:
	\rm -fr $(O_TF_A_DIR)

__PHONY += __atf-checkupdate
__atf-checkupdate:
	$(EELQ2)diff $(TF_A_BL32_BIN) $(O_TF_A_DIR)/qemu/$(TF_A_OUTDIR)/bl32.bin || $(MAKE_THIS) atf-clean
	$(EELQ2)diff $(TF_A_BL32_EXTRA1_BIN) $(O_TF_A_DIR)/qemu/$(TF_A_OUTDIR)/bl32_extra1.bin || $(MAKE_THIS) atf-clean
	$(EELQ2)diff $(TF_A_BL32_EXTRA2_BIN) $(O_TF_A_DIR)/qemu/$(TF_A_OUTDIR)/bl32_extra2.bin || $(MAKE_THIS) atf-clean
	$(EELQ2)diff $(TF_A_BL33_BIN) $(O_TF_A_DIR)/qemu/$(TF_A_OUTDIR)/bl33.bin || $(MAKE_THIS) atf-clean
	$(EELQ2)[ -f $(TF_A_IMAGE) ] || $(MAKE_THIS) atf
